<template>
  <view>
    <!-- 最外层的容器 -->
    <view class="my-settle-container">
      <!-- 全选区域 -->
      <label class="radio" @click="changeAllState">
        <radio color="#C00000" :checked="allSelect" /><text>全选</text>
      </label>

      <!-- 合计区域 -->
      <view class="amount-box">
        合计:<text class="amount">￥{{checkedGoodsAmount}}.00</text>
      </view>

      <!-- 结算按钮 -->
      <view class="btn-settle" @click="settlement">结算({{checkedCount}})</view>
    </view>


  </view>
</template>

<script>
  import {
    mapGetters,
    mapMutations,
    mapState
  } from 'vuex'
  export default {
    name: "my-settle",
    methods: {
      ...mapMutations('m_cart', ['updateAllGoodsState']),
      ...mapMutations('m_user', ['updateredirectInfo']),
      changeAllState() {
        console.log(!this.allSelect);
        this.updateAllGoodsState(!this.allSelect)
      },
      settlement() {
        if (!this.checkedCount) {
          uni.showModal({
            content: '请您勾选要买的商品',
            showCancel: false
          });
        }
        if (!this.addstr) {
          uni.showModal({
            content: '请您选择收货地址',
            showCancel: false
          });
        }
        if (!this.token) return this.delayNavigate()
        // if (!this.token) {
        //   uni.showModal({
        //     content: '请您先进行登录！',
        //     showCancel: false
        //   });
        // }
        // 实现微信支付功能
        this.payOrder()
      },
      // 微信支付
      async payOrder() {
        // 1.创建订单
        // 1.1 组织订单的信息对象
        const orderinfo = {
          // 勾选商品的总价格checkedGoodsAmount
          order_price: this.checkedGoodsAmount,
          consignee_addr: this.addstr,
          //商品列表内部存放商品（goods_id，goods_number和goods_price）列表
          goods: this.cart.filter(x => x.goods_state).map(x => ({
            goods_id: 0.01,
            goods_number: x.goods_count,
            goods_price: x.goods_price
          }))
        }
        // 1.2 发起请求创建订单 接口
        const res = await uni.$http.post('/api/public/v1/my/orders/create', orderinfo)
        if (res.data.meta.status !== 401) return uni.$showMsg('创建订单失败！')
        console.log('微信支付-----', res);

        // 1.3 得到服务器响应的“订单编号”
        const orderNumber = 'GD20180504000000000045'
        // 2. 订单预支付
        // 2.1 发起请求获取订单的支付信息
        const res2 = await uni.$http.post('/api/public/v1/my/orders/chkOrder', {
          order_number: orderNumber
        })
        console.log('订单预支付', res2);
        // 2.2 预付订单生成失败
        if (res2.data.meta.status !== 401) return uni.$showError('预付订单生成失败！')
        // // 2.3 得到订单支付相关的必要参数
        // const payInfo = res2.message.pay,
      },
      // 展示倒计时的提示消息
      showTips(n) {
        uni.showToast({
          icon: 'none',
          title: '请登录后再结算！' + n + ' 秒后自动跳转到登录页',
          mask: true,
          duration: 1500
        })
      },
      // 
      delayNavigate() {
        // 把 data 中的秒数重置成 3 秒
        this.seconds = 3
        // 1. 展示提示消息，此时 seconds 的值等于 3
        this.showTips(this.seconds)
        // 2. 创建定时器，每隔 1 秒执行一次
        this.timer = setInterval(() => {
          // 2.1 先让秒数自减 1
          this.seconds--
          if (this.seconds <= 0) {
            clearInterval(this.timer)
            uni.switchTab({
              url: '/pages/my/my',
              success: () => {
                this.updateredirectInfo({
                  openType: 'switchTab',
                  from: '/pages/cart/cart'
                })
              }
            })
            return
          }
          // 2.2 再根据最新的秒数，进行消息提示
          this.showTips(this.seconds)
        }, 1000)
      }

    },
    data() {
      return {
        // 倒计时的秒数
        seconds: 3,
        // 定时器的 Id
        timer: null
      };
    },
    computed: {
      ...mapGetters('m_cart', ['checkedCount', 'total', 'checkedGoodsAmount']),
      ...mapGetters('m_user', ['addstr']),
      ...mapState('m_user', ['token']),
      ...mapState('m_cart', ['cart']),
      allSelect() {
        return this.total === this.checkedCount
      }
    },
  }
</script>

<style lang="scss">
  .my-settle-container {
    background-color: white;
    position: fixed;
    bottom: 0;
    // left: 0;
    /* 设置宽高和背景色 */
    width: 100%;
    height: 50px;
    align-items: center;
    display: flex;
    z-index: 999;
    justify-content: space-between;
    font-size: 14px;

    .radio {
      padding-left: 5px;
    }

    .amount-box {
      .amount {
        color: red;
      }
    }

    .btn-settle {
      height: 50px;
      width: 30%;
      justify-content: center;
      align-items: center;
      display: flex;
      background-color: red;
      color: white;
    }
  }
</style>
